package com.gitee.wsl.mathematics.geometry.shape

import com.gitee.wsl.ext.math.Dimensions
import kotlin.math.abs
import kotlin.math.hypot

open class Dimension(var startPoint: Point = Point(0f,0f),
                     var endPoint: Point = Point(0f,0f)){

    constructor(length: Float):this(endPoint = Point(length,0f))

    val x1:Float by startPoint::x
    val y1:Float by startPoint::y

    val x2:Float by endPoint::x
    val y2:Float by endPoint::x

    val center: Point
        get() = Point((startPoint.x + endPoint.x) / 2, (startPoint.y + endPoint.y) / 2)

    val distance:Float
        get() {
        val dx = abs(endPoint.x - startPoint.x)
        val dy = abs(endPoint.y - startPoint.y)
        return hypot(dx, dy)
    }

    fun percent( percent: Float): Point = percent(percent,percent)

    fun percent( percent1: Float,percent2: Float ): Point {
        val x = (endPoint.x - startPoint.x) * percent1 + startPoint.x
        val y = (endPoint.y - startPoint.y) * percent2 + startPoint.y
        return Point(x, y)
    }

    fun percentEnd(percent: Float): Point = percent( 1.0f - percent )
}

fun Dimensions.isNearlyPoint(touchX: Float, touchY: Float, point: Point, minLength: Int): Boolean {
    return Point(touchX,touchY).distance(point) < minLength
}

infix fun Point.to(other: Point) = Dimension(this,other)